package com.ryder.ryderschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryder.ryderschool.entity.Project;
import com.ryder.ryderschool.entity.ProjectResult;
import com.ryder.ryderschool.entity.User;
import com.ryder.ryderschool.mapper.ProjectResultMapper;
import com.ryder.ryderschool.service.ProjectResultService;
import com.ryder.ryderschool.service.ProjectService;
import com.ryder.ryderschool.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ProjectResultServiceImpl extends ServiceImpl<ProjectResultMapper, ProjectResult> implements ProjectResultService {

    private final ProjectService projectService;
    private final UserService userService;

    @Override
    @Transactional
    public void createResult(ProjectResult result) {
        // 检查项目是否存在
        Project project = projectService.getById(result.getProjectId());
        if (project == null) {
            throw new RuntimeException("项目不存在");
        }

        // 检查是否是项目成员
        User currentUser = userService.getCurrentUser();
        if (!currentUser.getId().equals(project.getLeaderId())) {
            throw new RuntimeException("只有项目负责人可以创建成果记录");
        }

        save(result);
    }

    @Override
    @Transactional
    public void updateResult(ProjectResult result) {
        ProjectResult existingResult = getById(result.getId());
        if (existingResult == null) {
            throw new RuntimeException("成果记录不存在");
        }

        // 检查是否是项目负责人
        Project project = projectService.getById(existingResult.getProjectId());
        User currentUser = userService.getCurrentUser();
        if (!currentUser.getId().equals(project.getLeaderId())) {
            throw new RuntimeException("只有项目负责人可以修改成果记录");
        }

        updateById(result);
    }

    @Override
    @Transactional
    public void deleteResult(Integer id) {
        ProjectResult result = getById(id);
        if (result == null) {
            throw new RuntimeException("成果记录不存在");
        }

        // 检查是否是项目负责人
        Project project = projectService.getById(result.getProjectId());
        User currentUser = userService.getCurrentUser();
        if (!currentUser.getId().equals(project.getLeaderId())) {
            throw new RuntimeException("只有项目负责人可以删除成果记录");
        }

        removeById(id);
    }

    @Override
    public ProjectResult getResult(Integer id) {
        return getById(id);
    }

    @Override
    public Page<ProjectResult> listResults(Integer projectId, Integer pageNum, Integer pageSize) {
        return page(new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<ProjectResult>()
                        .eq(ProjectResult::getProjectId, projectId)
                        .orderByDesc(ProjectResult::getPublishedDate));
    }
} 